using System;
using System.Collections.Generic;
using System.Linq;

public  class PlayerStates
{
    // 基础属性（局外成长）
    public long BaseHp;
    public long BaseMp;
    public long BaseSp;
    public long BaseAtk;
    public long BaseDef;
    public long Base闪避;
    public long Base暴击率;
    public long Base爆伤;
    public long Base力量;
    public long Base敏捷;
    public long Base感知;
    public long Base魔力;
    public long Base耐力;
    public long Base幸运;
    public long Base魅力;

    // 当前属性（局内成长）
    public long Hp;
    public long Mp;
    public long Sp;
    public long Atk;
    public long Def;
    public long 闪避;
    public long 暴击率;
    public long 爆伤;
    public long 力量;
    public long 敏捷;
    public long 感知;
    public long 魔力;
    public long 耐力;
    public long 幸运;
    public long 魅力;

    // Buff/Debuff 系统
    public List<Buff> Buffs = new List<Buff>();

    // 构造函数
    public PlayerStates(long hp = 50, long mp = 10)
    {
        // 基础属性初始化
        BaseHp = hp;
        BaseMp = mp;
        BaseSp = 0;
        BaseAtk = 10;
        BaseDef = 0;
        Base闪避 = 10000 * 5;
        Base暴击率 = 10000 * 3;
        Base爆伤 = 150;
        Base力量 = 5;
        Base敏捷 = 5;
        Base感知 = 5;
        Base魔力 = 5;
        Base耐力 = 5;
        Base幸运 = 5;
        Base魅力 = 5;

        // 当前属性初始化
        ResetToBase();
    }

    // 将当前属性重置为基础属性（每次冒险开始时调用）
    public void ResetToBase()
    {
        Hp = BaseHp;
        Mp = BaseMp;
        Sp = BaseSp;
        Atk = BaseAtk;
        Def = BaseDef;
        闪避 = Base闪避;
        暴击率 = Base暴击率;
        爆伤 = Base爆伤;
        力量 = Base力量;
        敏捷 = Base敏捷;
        感知 = Base感知;
        魔力 = Base魔力;
        耐力 = Base耐力;
        幸运 = Base幸运;
        魅力 = Base魅力;
        Buffs.Clear();
    }

    // 受伤结算
    public void TakeDamage(long amount)
    {
        long damage = amount - Def;
        if (damage < 0) damage = 0;
        // 闪避判定
        if (UnityEngine.Random.value < (闪避 / 10000f)) return;
        Hp -= damage;
        if (Hp < 0) Hp = 0;
    }

    // 回血
    public void Heal(long amount)
    {
        Hp += amount;
        if (Hp > BaseHp) Hp = BaseHp;
    }

    // 添加Buff
    public void AddBuff(Buff buff)
    {
        Buffs.Add(buff);
        buff.OnApply?.Invoke(this);
    }

    // 回合开始时结算Buff
    public void ApplyBuffsOnTurnStart()
    {
        foreach (var buff in Buffs.ToList())
        {
            buff.OnTurnStart?.Invoke(this);
            buff.Duration--;
            if (buff.Duration <= 0)
            {
                buff.OnRemove?.Invoke(this);
                Buffs.Remove(buff);
            }
        }
    }

    // 回合结束时结算Buff
    public void ApplyBuffsOnTurnEnd()
    {
        foreach (var buff in Buffs.ToList())
        {
            buff.OnTurnEnd?.Invoke(this);
        }
    }
}

// Buff 基本结构
public class Buff
{
    public string Name;
    public int Duration; // 持续回合数
    public Action<PlayerStates> OnApply;
    public Action<PlayerStates> OnRemove;
    public Action<PlayerStates> OnTurnStart;
    public Action<PlayerStates> OnTurnEnd;
} 